<?php
parse_str(implode('&', array_slice($argv, 1)), $_GET);

$tweet_text = isset($_GET['string']) ? urldecode($_GET['string']) : '';
if($tweet_text == ''){
	return '{ "code" : "400" }';
} else {
	$img = isset($_GET['image']) ? urldecode($_GET['image']) : '';
	$result = post_tweet($tweet_text, $img);
	print $result;
}

function post_tweet($tweet_text, $image = '') {
	$customer_key = isset($_GET['consumer_key']) ? $_GET['consumer_key'] : '';
	$consumer_secret = isset($_GET['consumer_secret']) ? $_GET['consumer_secret'] : '';
	$user_token = isset($_GET['user_token']) ? $_GET['user_token'] : '';
	$user_secret = isset($_GET['user_secret']) ? $_GET['user_secret'] : '';
	if($customer_key == '' or $consumer_secret == '' or $user_token == '' or $user_secret == ''){
		return '{ "code" : "401" }';
	}
	
	require_once('tmhoauth/tmhOAuth.php');
	$connection = new tmhOAuth(array(
		'consumer_key' => $customer_key,
	    'consumer_secret' => $consumer_secret,
	    'user_token' => $user_token,
	    'user_secret' => $user_secret,
	));
	if($image != ''){
		$connection->request('POST', 
		'https://upload.twitter.com/1.1/statuses/update_with_media.json', 
		array('media[]'  => "@{$image}",'status'   => $tweet_text),
		true,
		true
		);
		return '{ "code" : "'.$connection->response['code'].'" , "response" : '.$connection->response['response'].' }';
	} else {
		$connection->request('POST', 
		$connection->url('1.1/statuses/update'), 
		array('status' => $tweet_text));
		//$string = '';
		//foreach($this->response as $key => $val){
		//	$string .= $key.' - ';
		//}
		//return $string;	  
		return '{ "code" : "'.$connection->response['code'].'" , "response" : '.$connection->response['response'].' }';
	}
}
?>
